<!DOCTYPE html>
<head>
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  <title>{% block title %}{% endblock %} Failure</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="/common.css">
  <script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
  <script src="/ui/js/common.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link rel="import" href="/ui/elements/cats-app.html">
  {% block head %}{% endblock head %}
  <style>
    .running {
      color: #666666;
      background-color: #fffc6c;
      border-color: #c5c56d;
    }
    .completed {
      color: #ffffff;
      background-color: #8fdf5f;
      border-color: #4f8530;
    }
    .error {
      color: #ffffff;
      background-color: #e98080;
      border-color: #a77272;
    }
    .hidden {
      display: none;
    }
    {% block style %}{% endblock style %}
  </style>
  <script>
    var findit = {};
    findit.analysisCompleted = '{{analysis_completed}}' == 'True';
    findit.analysisFailed = '{{analysis_failed}}' == 'True';
    findit.builderUrl = 'https://luci-milo.appspot.com/buildbot/{{master_name}}/{{builder_name}}';
    findit.buildUrl = findit.builderUrl + '/{{build_number}}';
    findit.showDebugInfo = '{{show_debug_info}}' == 'True';
    findit.xsrfToken = '{{ xsrf_token }}';

    {% block constant %}{% endblock constant %}

    function handleLogin(httpStatus) {
      if (httpStatus == 401 || httpStatus == 403) {
        displayMessage(100); // Permission error.
      } else {
        displayMessage(null, 'Unknown error. Please refresh the page and try again!');
      }
    }

    function triageSuspectedCL(e) {
      var parameters = {
        'format': 'json',
        'cl_info': e.name,
        'status': e.value,
        'url': findit.buildUrl,
        'xsrf_token': findit.xsrfToken,
      };
      $.post('/waterfall/triage-suspected-cl', parameters, function(data) {
        if (! data['success']) {
          displayMessage(null, 'Failed to update datastore. Please refresh and try again.');
        } else {
          $('#cl-result-triaged').text('Feedback has been recorded. Thank you!')
        }
      }).fail(function(xhr) {
        handleLogin(xhr.status);
      });
    }

    {% block functions %}{% endblock functions %}

    $(function() {
      document.getElementById('app').userInfo = {{ (user_info or {}) | tojson | safe }};

      if (!findit.analysisCompleted) {
        $('#status_message').text('running, will refresh in 5 seconds...');
        $('#status_message').attr('class', 'running');
        setTimeout(function() {
          {% if show_debug_info %}
            window.location.href = 'failure?url=' + findit.buildUrl + '&debug=1';
          {% else %}
            window.location.href = 'failure?url=' + findit.buildUrl;
          {% endif %}
        }, 5000);
      } else {
        if (findit.analysisFailed) {
          $('#status_message').text('error');
          $('#status_message').attr('class', 'error');
        } else {
          $('#status_message').text('completed');
          $('#status_message').attr('class', 'completed');

          {% block failure_classification %}{% endblock failure_classification %}
        }
      }

      $('.score-info').click(function() {
        displayMessage(200); // Score Explanation.
      });
    });
  </script>
</head>
<body>
  <cats-app id="app" components="Tools>Test>FindIt" page-header="Findit: Analysis Result">
    <b>{% block heading %}{% endblock heading %}</b>
    <div>
      Master: {{master_name}}<br>
      Builder: <a href="https://luci-milo.appspot.com/buildbot/{{master_name}}/{{builder_name}}" target="_blank">{{builder_name}}</a><br>
      {% block failure_info %}{% endblock failure_info %}
    </div>
    <br>

    <b>Heuristic analysis info:</b>
    <div id="analysis-info">
      status: <span id="status_message"></span>
      {% if show_debug_info %}
        {% if pipeline_status_path %}
          <a href="{{pipeline_status_path}}" target="_blank">pipeline</a>
        {% endif %}
        <br>
        Requested: {{analysis_request_time | default('N/A', true)}}<br>
        {% if analysis_duration %}
          Duration: {{analysis_duration}} seconds. <br>
        {% else %}
          Started: {{analysis_start_time | default('N/A', true)}}<br>
        {% endif %}
        Updated: {{analysis_update_time}}<br>
      {% endif %}
    </div>
    <br>

    <b>Findit result:</b>

    {% block try_job_result %}{% endblock try_job_result %}
    {% if analysis_completed %}
        {% block result_tables %}{% endblock result_tables %}
    {% endif %}

    {% if analysis_completed %}
    <br>
    <div>
      <div>
        {% if suspected_cls %}
          <b>Feedback appreciated:</b>
          {% if (user_info or {}).get('email') %}
            (if provided, it connects to your email for 30 days)
          {% endif %}
          <table>
            <tr><th>CL</th><th>Confidence</th><th>Feedback</th></tr>
            {% for suspected_cl in suspected_cls %}
              <tr>
                <td>{{ suspected_cl.repo_name }}&nbsp;<a href="{{ suspected_cl.url }}" target="_blank">{{ suspected_cl.commit_position or suspected_cl.revision }}</a></td>
                <td>{{ suspected_cl.confidence }}</td>
                <td>
                  {% set cl_info = suspected_cl.repo_name + '/' + suspected_cl.revision %}
                  <input type="radio" name="{{ cl_info }}" value="0" {{'checked' if suspected_cl.status == 0 else ''}} onchange="triageSuspectedCL(this)">Correct &nbsp;&nbsp;
                  <input type="radio" name="{{ cl_info }}" value="1" {{'checked' if suspected_cl.status == 1 else ''}} onchange="triageSuspectedCL(this)">Incorrect
                </td>
              </tr>
            {% endfor %}
          </table>
        {% endif %}
        <div id="cl-result-triaged"></div>
      </div>
      {% if show_debug_info and analysis_is_duplicate %}
        <div>
          This analysis was automatically triaged as a duplicate of another build analysis: <a href="/waterfall/failure?url=https://luci-milo.appspot.com/buildbot/{{triage_reference_analysis_master_name}}/{{triage_reference_analysis_builder_name}}/{{triage_reference_analysis_build_number}}">{{triage_reference_analysis_master_name}}/{{triage_reference_analysis_builder_name}}/{{triage_reference_analysis_build_number}}</a>
        </div>
      {% endif %}
    </div>
    {% endif %}

    {% if show_admin_controls %}
      <br><br>
      <div id="rerun-analysis">
          <form id="rerun-form" action="/waterfall/failure" method="post">
              <input type="hidden" name="xsrf_token" value="{{xsrf_token}}"/>
              <input type="hidden" name="force" value="1"/>
              <input type="hidden" name="url" value="https://luci-milo.appspot.com/buildbot/{{master_name}}/{{builder_name}}/{{build_number}}" />
              <input type="submit" value="Force Rerun Analysis" />
          </form>
      </div>
    {% endif %}

    {% if triage_history %}
      <br>
      <b>Triage history:</b>
      <div>
        <table>
          <tr><th>When</th><th>Who</th><th>CL</th><th>Result</th><th>Version</th></tr>
          <tbody>
            {% for triage_record in triage_history %}
              <tr>
                <td>{{triage_record.triage_time}}</td>
                <td>{{triage_record.user_name}}</td>
                <td>{{triage_record.triaged_cl.repo_name}} <a href="{{triage_record.triaged_cl.url}}" target="_blank">{{triage_record.triaged_cl.commit_position or triage_record.triaged_cl.revision}}</a></td>
                <td>{{triage_record.result_status}}</td>
                <td>{{triage_record.version | default('N/A', true)}}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    {% endif %}
  </cats-app>
</body>
